package com.architecture.util.collection;

import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.architecture.util.object.ObjectUtility;

/**
 * <p>This class has utilities methods for be used with collections (Java Collections Framework - JCF).</p>
 * @author JR
 * @version 1.0
 * @category Collections Utilities
 */
public final class CollectionUtility {
 
	/**
	 * private constructor of <code>CollectionUtility</code>.
	 */
	private CollectionUtility() {
		super();
	}
	
	/**
	 * <p>Verify if the collection is empty.</p>
	 * @param collection
	 * @return <code>Boolean</code>
	 */
	public static Boolean isEmpty(final Collection<?> collection){
		if(ObjectUtility.isRef(collection)){
			throw new NullPointerException();
		}
		return collection.isEmpty();
	}
	
	/**
	 * <p>Verify if the collection is not empty.</p>
	 * @param collection
	 * @return <code>Boolean</code>
	 */
	public static Boolean isNotEmpty(final Collection<?> collection){
		if(ObjectUtility.isRef(collection)){
			throw new NullPointerException();
		}
		return !isEmpty(collection);
	}
	
	/**
	 * <p>Gets the collection length, If the collection is null, returns -1 as length.</p>
	 * @param collection
	 * @return <code>Integer</code>
	 */
	public static Integer length(final Collection<?> collection){
		if(ObjectUtility.isNotRef(collection)){
			return collection.size();
		}else{
			return -1;
		}
	}
	
	/**
	 * <p>Order a list using a comparator.</p>
	 * @param collection
	 * @param comparator
	 */
	public static <T> void order(final List<T> collection, final Comparator<T> comparator){
		if(ObjectUtility.isAllRef(collection,comparator)){
			Collections.sort(collection, comparator);	
		}
	}
	
	/**
	 * <p>Build a reverse list.</p>
	 * @param list
	 */
	public static void reverse(final List<?> list){
		Collections.reverse(list);
	}
	
}
